/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package numberpickerfx;

import java.net.URL;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Platform;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.*;
import javafx.scene.layout.AnchorPane;
import numberpickerfx.logic.*;

/**
 *
 * @author zengchen
 */
public class FXMLDocumentController implements Initializable {
    @FXML
    AnchorPane pane;
    @FXML
    Button start, next;
    @FXML
    Slider maxSlider, timeSlider;
    @FXML
    Label maxSliderCount, timeSliderCount, display;
    @FXML
    CheckBox allowRepeat, smartMode;
    @FXML
    ProgressIndicator progress;
    
    private Thread check;
    private int max, time;
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        next.setVisible(false);
        check = new Thread(() -> {
            while(true) {
                //System.out.println("hhh");
                max = (int) (maxSlider.getValue());
                time = (int) (timeSlider.getValue());
                Platform.runLater(() -> {
                    maxSliderCount.setText(String.valueOf(max));
                    timeSliderCount.setText(String.valueOf(time));
                    setProgress();
                });
                try {
                    Thread.sleep(50);
                } catch (InterruptedException ex) {
                    Logger.getLogger(FXMLDocumentController.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
        check.start();
    }    
    
    @FXML
    private void onExit() {
        System.exit(0);
    }
    
    @FXML
    private void maxvalueChange() {
        maxSliderCount.setText(String.valueOf(maxSlider.getValue()));
        //maxSlider.set
    }
    
    @FXML
    private void timevalueChange() {
        
    }
    
    protected IRandomNumber gen;
    protected int count = 0;
    @FXML
    private void onStart() {
        //System.exit(0);
        gen = new RandomNumberNoRepeat(1, max);
        next.setVisible(true);
        count = 0;
    }
    
    @FXML
    private void onNext() {
        if(count < time - 1) {
            display.setText(gen.getNumber()+"");
            count++;
        } else {
            if(count == time - 1){
                count++;
                next.setVisible(false);
            }
        }
        setProgress();
    }
    
    private void setProgress() {
        progress.setProgress((double)count / time);
    }
}
